Ext.define('PJ.view.celabankmanagement.BankManagementList', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.celabankManagementList'],
    requires: ['AM.default.DefaultSearchForm', 
               'AM.default.DateTime', 
               'AM.default.DefaultDateField'
               ],
    
    isPossibleLoad: true,
    initComponent: function(){
    	var me = this;
        me.columns = [{
            header: '试题题干',
            dataIndex: 't.questionName',
            flex: 1,
            minWidth: 120,
			renderer: function(value, metaData, record){
				metaData.tdAttr = 'data-qtip="' + value + '"';
				return value;
			}
        }/*, {
            header: '试题题型',
            dataIndex: 't.questionType',
            width: 70,
            renderer: function(value, metaData, record){
                //metaData.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }*/, {
            header: '试题题组',
            dataIndex: 't.questionsProblemSets',
            width: 90,
            renderer: function(value, metaData, record){
                //metaData.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '试题类型',
            dataIndex: 't.questionType',
            width: 90,
            renderer: function(value, metaData, record){
                //metaData.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '创建时间',
            dataIndex: 't.createDate',
            width: 130
        }];
        me.store = Ext.create('PJ.'+ prototype +'.celabankmanagement.BankManagementList', {
            isLoad: false
        });
        me.callParent(arguments);
    },
    
    //新增
    onAddClick: function(button){
        var me = this;
        /*
        var win = Ext.create('PJ.view.celabankmanagement.QuestionsEdit', {
            grid: this,
            notEdit: true,
            title: "新增试题"
        }).show();
        */
        var win = Ext.create('PJ.view.celabankmanagement.Edit', {
            grid: this,
            notEdit: true,
            title: "新增试题"
        }).show();
    },
    
    //更新
    onUpdateClick: function(button){
    	var me = this;
        var record = this.getSelectionModel().getSelection();
        if (record[0].get("isSelected") == 1) {
            Ext.Msg.show({
                title: '警告',
                msg: '已被选为调查问卷试题且已发布，不允许修改！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }else if (record[0].get("isSelected") == 2) {
        	Ext.Msg.confirm({
                title: '提示',
                msg: "已被选为调查问卷试题，确定要修改？",
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
                    	Ext.create('PJ.view.celabankmanagement.Edit', {
                            grid: me,
                            isReadOnly:false,
                            isEdit: false,
                            notEdit: false,
                            title: "修改试题",
                            questionId:record[0].get("questionId")
                        }).show();
                    }
                    else {
                        return;
                    }
                }
            });
        }else{
        	Ext.create('PJ.view.celabankmanagement.Edit', {
                grid: me,
                isEdit: false,
                notEdit: false,
                isReadOnly:false,
                title: "修改试题",
                questionId:record[0].get("questionId")
            }).show();
        }
    },
    // 删除
    onDeleteClick: function(button){
        var me = this;
        var store = me.getStore();
        var records = me.getSelectionModel().getSelection();
        var message="";
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("isSelected") == 1) {
                Ext.Msg.show({
                    title: '提示',
                    msg: '已被选为调查问卷试题且已发布，不允许删除！',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
        for (var i = 0; i < records.length; i++) {
            if(records[i].get("isSelected") == 2){
            	message="已被选为调查问卷试题，确定要删除？";
            	break;
            }
        }
        Ext.getCmp("loadingMask").show();
        Ext.Msg.confirm({
            title: '提示',
            msg: Ext.isEmpty(message)==true?'您确定要删除这些信息吗?':message,
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id){
                if (id == 'ok') {
                    Ext.getCmp("loadingMask").show();
                    var questionIds = new Array();
                    Ext.Array.each(records, function(name, index){
                    	questionIds.push(name.get("questionId"));
                    });
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
        					loadType: gj_onlineLearning_constant.loadType,
        					url: me.getStore().deleteUrl,
        					extraParams: {
        						questionIds: questionIds
        					}
        				});
        				store.load(function(rec, operation, success){
        					Ext.getCmp("loadingMask").hide();
        					if(success){
        						Orchid.alert('消息', '删除成功！', function(){
        							var store = me.getStore();
        							store.load();
        						});
        					}else{
        						Orchid.alert('消息', operation.error);
        					}
        				});
                    }else if(gj_onlineLearning_constant.loadType == "ajax"){
                    	Ext.Ajax.request({
                            url: me.getStore().deleteUrl,
                            params: {
                            	questionIds: questionIds
                            },
                            success: function(response){
                                Ext.getCmp("loadingMask").hide();
                                var data = response.responseText;
                                var json = Ext.decode(data, true);
                                if (json.success) {
                                    Ext.Msg.alert('消息', '删除成功！', function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                                else {
                                    Ext.Msg.alert('消息', json.msg, function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                            }
                        });
                    }
                }
                else {
                	Ext.getCmp("loadingMask").hide();
                    return;
                }
            }
        });
    },
    listeners: {
    	viewready: function(){
    		var me = this;
    		this.addDocked([{
    			dock: 'top',
    			xtype: "searchform",
    			columnCount: 3,
    			fieldDefaults: {
    				labelAlign: 'right',
    				labelWidth: 70
    			},
    			items: [{
    				xtype: "textfield",
    				fieldLabel: '试题题干',
    				name: "questionName",
    				maxLength: "500",
    				emptyText: "请输入试题题干"
    			}, {
                    xtype: "combo",
                    fieldLabel: '试题题型',
                    name: "questionType",
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: gj_onlineLearning_constant.loadType == "jsonp" ? gj_onlineLearning_constant.loadType:'ajax',
                            url : me.store.typeUrl,
                            xtype: 'combo', 
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    emptyText: "请选择"
                }, {
    				xtype: 'defaultDate',
    				fieldLabel: '创建时间',
    				name: 'createDateStart',
    				id: 'searchBankSD',
    				vtype: 'startLessEndDate',
    				startLessEndDate: {
    					targetCmpId: 'searchBankED',
    					isStart: true
    				},
    				emptyText: "请输入创建开始时间"
    			}, {
    				xtype: 'defaultDate',
    				fieldLabel: '～',
    				labelSeparator: '',
    				name: 'createDateEnd',
    				id: 'searchBankED',
    				vtype: 'startLessEndDate',
    				vtypeText: '结束时间不能早于开始时间！',
    				startLessEndDate: {
    					targetCmpId: 'searchBankSD',
    					isStart: false
    				},
    				emptyText: "请输入创建结束时间"
    			}],
    			grid: this
    		}, {
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [this.addButton, "-", this.updateButton, "-", this.deleteButton, "->", this.countSelectPanel]
            }]);
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    		var store = this.store;
            var params = store.proxy.extraParams;
            var newParams = {};
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load();
    	}
    }
});
